package xwc.usertree;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.math.BigDecimal;

public class AccountLevels {

    /**
     * 入金通道: 1-1000渠道； 2-3000渠道；3-5000渠道
     * 达到3000的渠道的条件： 邀请6个有效用户
     * 达到v2时开通5000渠道
     */
    public static final int ONE_CHANNEL = 1;
    public static final int TWO_CHANNEL = 2;
    public static final int THREE_CHANNEL = 3;


    /**
     * 达到v1的条件：邀请6个有效用户 + 当前用户复投
     * 推6人成为v1，满足v1条件且除自己外团队业绩20万为v2，满足v1条件且除自己外团队业绩100万为v2
     */
    public static final int V0 = 0;
    public static final int V1 = 1;
    public static final int V2 = 2;
    public static final int V3 = 3;


    @AllArgsConstructor
    @Data
    public static class LevelInfo {
        private BigDecimal minChildrenTreeDepositTotal; // 级别要求的children树最低总充值金额
        private BigDecimal dividendPercentage; // 动态收益提成比例
    }

    /**
     * 获取各级别要求的children树最低总充值金额和动态收益提成比例
     *
     * @param level
     */
    public static LevelInfo getLevelInfo(int level) {
        switch (level) {
            case V0:
                return new LevelInfo(BigDecimal.ZERO, BigDecimal.ZERO);
            case V1:
                return new LevelInfo(Config.v1Amount, Config.v1Percentage);
            case V2:
                return new LevelInfo(Config.v2Amount, Config.v2Percentage);
            case V3:
                return new LevelInfo(Config.v3Amount, Config.v3Percentage);
            default:
                return new LevelInfo(BigDecimal.ZERO, BigDecimal.ZERO);
        }
    }

    /**
     * 当账户下级充值并有 {intoDividendPoolAmount} 金额进入下级的个人分红池时，本级最多能释放的分红池金额
     *
     * @param account
     * @param intoDividendPoolAmount
     * @return
     */
    public static BigDecimal getParentFreeAmountWhenNewChild(Account account, BigDecimal intoDividendPoolAmount) {
        Integer newChildCountInCurrentDeposit = account.getNewChildCountInCurrentDeposit();
        if (newChildCountInCurrentDeposit == null || newChildCountInCurrentDeposit == 0) {
            return BigDecimal.ZERO;
        }
//        if (newChildCountInCurrentDeposit == 1) {
//            return new BigDecimal("0.1").multiply(intoDividendPoolAmount);
//        }
//        if (newChildCountInCurrentDeposit == 2) {
//            return new BigDecimal("0.3").multiply(intoDividendPoolAmount);
//        }
//        if (newChildCountInCurrentDeposit == 3) {
//            return new BigDecimal("0.6").multiply(intoDividendPoolAmount);
//        }
//        if (newChildCountInCurrentDeposit == 4) {
//            return new BigDecimal("0.1").multiply(intoDividendPoolAmount);
//        }
//        if (newChildCountInCurrentDeposit == 5) {
//            return new BigDecimal("0.3").multiply(intoDividendPoolAmount);
//        }
//        if (newChildCountInCurrentDeposit == 6) {
//            return new BigDecimal("0.6").multiply(intoDividendPoolAmount);
//        }
        if (newChildCountInCurrentDeposit % 3 == 1) {
            return new BigDecimal("0.1").multiply(intoDividendPoolAmount);
        }
        if (newChildCountInCurrentDeposit % 3 == 2) {
            return new BigDecimal("0.3").multiply(intoDividendPoolAmount);
        }
        if (newChildCountInCurrentDeposit % 3 == 0) {
            return new BigDecimal("0.6").multiply(intoDividendPoolAmount);
        }
        return new BigDecimal("0.1").multiply(intoDividendPoolAmount);
    }

    /**
     * 获取{total}金额对应的账户级别
     *
     * @param total
     * @return
     */
    public static int getLevelOfTotalChildrenDeposited(Account account, BigDecimal total) {
        if (account.getLevel() <= V0 && account.getNewChildCountInCurrentDeposit() < Config.v1MinChildrenCount) {
            return V0;
        }
        if (total.compareTo(Config.v1Amount) < 0) {
            return V0;
        }
        // 根据2.10 彭和高睿说法，成为VIP1条件可能要改成邀请6个人并且复投。这里要判断account的累计充值金额 > 1000，在addNewDeposit的时候也要调用这个方法更新账户级别
        if (account.getDepositedTotal().compareTo(new BigDecimal(1000)) <= 0) {
            return V0;
        }
        if (total.compareTo(Config.v2Amount) < 0) {
            return V1;
        }
        if (total.compareTo(Config.v3Amount) < 0) {
            return V2;
        }
        return V3;
    }

    // 获取用户可以使用的通道
    public static int getAccountChannel(Account account) {
        if (account.getLevel() >= V2) {
            return THREE_CHANNEL;
        }
        if (account.getNewChildCountInCurrentDeposit() >= 6) {
            return TWO_CHANNEL;
        }
        return ONE_CHANNEL;
    }

}
